<template>
  <!-- 侧边栏 -->
  <div class="sidebar" :class="{ open: sidebarOpen }">
    <!-- 系统图标及名称 -->
    <div class="hospital-brand">
      <div class="hospital-icon">
        <img :src="logoUrl" alt="">
      </div>
      <div class="brand-text">智医云联</div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <router-link
        v-for="item in menuItems"
        :to="item.path"
        :key="item.key"
        custom
        v-slot="{ navigate, isActive }"
      >
        <div class="nav-item" :class="{ active: isActive }" @click="navigate">
          <div class="nav-icon">
            <el-icon><component :is="item.icon" /></el-icon>
          </div>
          <div class="nav-text">{{ item.name }}</div>
        </div>
      </router-link>
    </div>
    <!-- 用户信息及操作 -->
    <div class="user-profile">
      <el-dropdown trigger="click" @command="handleCommand">
        <div class="user-avatar">
          <el-icon><User /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人资料</el-dropdown-item>
            <el-dropdown-item>账户设置</el-dropdown-item>
            <el-dropdown-item divided @click="logout"
              >注销登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <div class="user-info">
        <div class="user-name">{{ username }}</div>
        <div class="user-role">心血管内科</div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted,computed } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

import { 
  ElNotification,
  ElMessage,
  ElButton,
  ElIcon
} from 'element-plus';

const sidebarOpen = ref(false);
const router = useRouter();
const store = useStore();

//定义子组件属性设置菜单活动样式
defineProps({
  activeMenu: {
    type: String,
    default: "dashboard", // 默认值
  },
});

// logo图标
const logoUrl = require("@/assets/logo/logo_h.png");

// 配置菜单项
const menuItems = ref([
  { name: "工作台", path: "/nurse/home", icon: "House", key: "dashboard" },
  {
    name: "预约管理",
    path: "/nurse/reservation",
    icon: "Calendar",
    key: "Reservation",
  },
  { name: "患者管理", path: "/nurse/patientmanager", icon: "User", key: "patient" },
  { name: "病历管理", path: "/nurse/medicalrecord", icon: "Document", key: "MedicalRecord" },  
  { name: "系统设置", path: "/settings", icon: "Setting", key: "settings" },
]);

// 当前激活的菜单项
const activeMenu = computed(() => {
  return useRouter.path;
});

const isActive = (path) => {
  if (path === "/" && activeMenu.value !== "/") {
    return false;
  }
  return activeMenu.value === path;
};

const username = ref(store.state.user?.username || "管理员");

const toggleSidebar = () => {
  sidebarOpen.value = !sidebarOpen.value;
};

const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      // 处理个人资料点击
      router.push('/nurse/profile');
      break;
    case 'settings':
      // 处理账户设置点击
      router.push('/nurse/settings');
      break;
    default:
      // 其他情况
      break;
  }
};

const logout = () => {
  // 清除本地存储的token
  localStorage.clear();
  // 重定向到登录页面
  router.push("/login");
  ElMessage.success("已安全退出系统");
};
</script>
<style lang="scss" scoped>
//设置本地字体
@font-face {
  font-family: 'MyBrushFont';
  src: url('@/assets/fonts/W04.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* 侧边栏样式 */
.sidebar {
  width: 250px;
  background: linear-gradient(135deg, #1a75bc 0%, #0c4d8c 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.hospital-brand {
  display: flex;
  align-items: center;
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 20px;
}

.hospital-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;

  img {
    width: 50px;
    height: 50px;
  }
}

.brand-text {
  font-family: 'MyBrushFont', 'ZCOOL XiaoWei', serif;
  font-size: 22px;  
  cursor: pointer;
}

.nav-menu {
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }

  &.active {
    background: rgba(255, 255, 255, 0.2);
    border-left: 4px solid white;
  }
}

.nav-icon {
  margin-right: 12px;
  width: 20px;
  text-align: center;
}

.user-profile {
  position: relative;
  padding: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-evenly;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  cursor: pointer;
}

.user-info {
  /*flex: 1;*/
}

.user-name {
  font-weight: 500;
  font-size: 14px;
}

.user-role {
  font-size: 12px;
  opacity: 0.8;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .user-profile {
    .logout-icon {
      display: none;
    }
  }

  .sidebar {
    width: 80px;
  }

  .brand-text,
  .nav-text,
  .user-info {
    display: none;
  }

  .hospital-brand {
    justify-content: center;
    padding: 15px 0;
  }

  .nav-item {
    justify-content: center;
    padding: 16px 0;
  }

  .nav-icon {
    margin-right: 0;
  }

  .user-profile {
    justify-content: center;
  }
}

@media (max-width: 768px) {
  .stats-cards {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    left: -80px;
    height: 100%;
    z-index: 1000;
    transition: left 0.3s;
  }

  .sidebar.open {
    left: 0;
  }

  .menu-toggle {
    display: block !important;
  }
}

.menu-toggle {
  display: none;
  margin-right: 15px;
  cursor: pointer;
}
</style>